package cn.edu.ysu.zry.handle;

import cn.edu.ysu.zry.config.ConfigZhongJiedian;
import cn.edu.ysu.zry.config.ConfigZhongxin;
import cn.edu.ysu.zry.entity.ZhongJiedian;
import cn.edu.ysu.zry.network.MessagePack;
import cn.edu.ysu.zry.network.RequestPack;

import java.util.Base64;
import java.util.Scanner;

/**
 * @program: XieyiBlockChainOS
 * @description: 操作类 - 重节点
 * @author: Ruiyuan Zhang
 * @create: 2020-04-21 15:06
 */
public class HandleZhongJiedian {
    private static class HANDLE{
        public static final String ADD_ZHONGJIEDIAN="addZhongJiedian";
        public static final String SHOW_ZHONGJIEDIAN_LIST="showZhongJiedianList";
        public static final String EXIT="exit";
    }
    /**
    *@Description: 启动重节点的操作类 - 主要控制方法的分发
    *@Author: Ruiyuan Zhang
    *@date: 2020/4/21
    */
    public static void startup(){
        Scanner input=new Scanner(System.in);
        System.out.println("                                       ");
        System.out.println("---------------------------------------");
        System.out.println("   [XieyiBlockChainOS - ZhongJiedian]  ");
        System.out.println("                                       ");
        System.out.println("              Ruiyuan Zhang            ");
        System.out.println("                  v0.1                 ");
        System.out.println("               2020-04-22              ");
        System.out.println("                                       ");
        System.out.println("---------------------------------------");
        System.out.println("                                       ");
        while (true){
            String str=input.next();
            String handleStrs[]=str.split(" ");
            for (String x:handleStrs) {
                x=x.trim();
            }
            String handleStr=handleStrs[0];
            if(handleStr.equals(HANDLE.ADD_ZHONGJIEDIAN)){
                addZhongJiedian();
            }else if(handleStr.equals(HANDLE.SHOW_ZHONGJIEDIAN_LIST)){

            }else if(handleStr.equals(HANDLE.EXIT)){
                break;
            }else if(handleStr.equals(HANDLE.EXIT)) {
                break;
            }else if(handleStr.equals(HANDLE.EXIT)){
                break;
            }
        }
        input.close();
    }
    /**
    *@Description: 申请添加重节点的逻辑处理类
    *@Param:
    *@return:
    *@Author: Ruiyuan Zhang
    *@date: 2020/4/21
    */
    public static void addZhongJiedian(){
        // 1 整理自己的资料封装成要发送的[ZhongJiedian]对象
        ZhongJiedian zhongJiedian=new ZhongJiedian();
        zhongJiedian.setIp(ConfigZhongJiedian.ip);
        zhongJiedian.setPort(ConfigZhongJiedian.port);
        zhongJiedian.setPublicKey(ConfigZhongJiedian.publicKey);

        // 2 进一步封装成[MessagePack]对象
        MessagePack<ZhongJiedian> messagePack=new MessagePack<>();
        messagePack.setMessage(MessagePack.MESSAGE_ZHONGXIN.LINK_TO);
        messagePack.setData(zhongJiedian);
        messagePack.setPublicKey(ConfigZhongJiedian.publicKey);

        // 3 调用请求对象发送[MessagePack]对象
        RequestPack.ip=ConfigZhongJiedian.ZhongXin.ip;
        RequestPack.port=ConfigZhongJiedian.ZhongXin.port;
        RequestPack.request(messagePack);

    }
}
